package repository

import (
    "hitme/go-api/models"
)

type photoSpecRep struct {
    BaseRepo
}

var PhotoSpecRep = &photoSpecRep{BaseRepo: BaseRepo{
    IModel: models.OssFileModel,
}}

func (r *photoSpecRep) FindById(id uint64) models.PhotoSpec {
    var userInfo models.PhotoSpec
    r.Model().Where("id = ?", id).First(&userInfo)
    return userInfo
}

// GetCateList 获取分类列表
func (r *photoSpecRep) GetCateList() []string {
    var data []string
    r.Model().Group("cate_name").Pluck("cate_name", &data)

    return data
}

func (r *photoSpecRep) GetList(isHot int8, SpecName string, CateName string, limit int, offset int) ([]models.PhotoSpec, int64) {
    var data []models.PhotoSpec
    query := r.Model()
    if isHot == 1 {
        query.Where("is_hot = ?", isHot)
    }
    if SpecName != "" {
        query.Where("spec_name like ?", "%" + SpecName + "%")
    }
    if CateName != "" {
        query.Where("cate_name = ?", CateName)
    }

    query.Limit(limit).Offset(offset).Order("id asc").Find(&data)

    var count int64
    query.Count(&count)

    return data, count
}

func (r *photoSpecRep) Create(data models.PhotoSpec) error {
    return r.Model().Create(&data).Error
}

func (r *photoSpecRep) Update(id uint64, data models.PhotoSpec) error {
    return r.Model().Where("id = ?", id).Updates(&data).Error
}

func (r *photoSpecRep) GetSpecNameByIds(ids []uint64) map[uint64]string {
    var data []models.PhotoSpec
    r.Model().Where("id in (?)", ids).Select("id, spec_name").Find(&data)
    res := make(map[uint64]string, 0)
    for _, val := range data {
        res[val.Id] = val.SpecName
    }
    return res
}